﻿#include "PaletteEditEntry.h"
#include "LumDebug.h"

#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>

struct PaletteEditEntry::PaletteEditEntryPrivate {
    QPointer<QLabel> labelInfo;
    QPointer<QPushButton> buttonEdit;
    QPointer<QWidget> widget;
};

PaletteEditEntry::~PaletteEditEntry() { }

void PaletteEditEntry::setWidget(QWidget *widget) {
    p->widget = widget;
    setValue(p->widget->palette());
}

PaletteEditEntry::PaletteEditEntry(QWidget *parent) : EditEntry{parent} {
    p.reset(new PaletteEditEntryPrivate);
    initUi();
    retranslateUi();
}

void PaletteEditEntry::setValue(const QPalette &value) {
    if(!p->widget) {
        return ;
    }

    if(p->widget->autoFillBackground()) {
        p->labelInfo->setText(tr("inhertits"));
        return ;
    }

    int modifiedCount = 4;
    p->labelInfo->setText(tr("Customized(%1roles)").arg(modifiedCount));
}

QPalette PaletteEditEntry::value() const {
    QPalette palette;
    return palette;
}

void PaletteEditEntry::onEditPalette() {
    DEBUG ;
}

void PaletteEditEntry::onReset() {
    DEBUG ;
}

void PaletteEditEntry::initUi() {
    p->buttonEdit = new QPushButton;
    connect(p->buttonEdit.data(), &QPushButton::clicked, this, &PaletteEditEntry::onEditPalette);
    p->labelInfo = new QLabel;

    const auto editLayout = new QHBoxLayout;
    editLayout->setContentsMargins(0, 0, 0, 0);
    editLayout->addWidget(p->buttonEdit);
    editLayout->addWidget(p->labelInfo);

    const auto container = new QWidget;
    container->setLayout(editLayout); 

    addEditControll(container);

    p->buttonEdit->hide();
}

void PaletteEditEntry::retranslateUi() {
    p->buttonEdit->setText(tr("Edit palette"));
}
